//
//  TechSelectViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/18.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "TechSelectViewController.h"

#import "BoxSelectViewController.h"

#import "PrintTypeTableViewCell.h"

@interface TechSelectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger selectType;

@end

@implementation TechSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"工艺选择";
    self.dataArray = [NSMutableArray array];
    self.selectType = 0;
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49)) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = ScreenX375(158);
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    UIButton *connectBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), ScreenX375(258), ScreenX375(49))];
    connectBtn.backgroundColor = REXADECIMALCOLOR(@"#ff9500");
    [connectBtn setTitle:@"有困难请联系我" forState:UIControlStateNormal];
    [connectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    connectBtn.titleLabel.font = Kfont(14);
    [self.view addSubview:connectBtn];
    
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(258), SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), SCREEN_WIDTH - ScreenX375(258), ScreenX375(49))];
    nextBtn.backgroundColor = REXADECIMALCOLOR(@"#ed223b");
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = Kfont(14);
    [nextBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
    
    [self getData];
}

- (void)nextStep {
    PrintTypeModel *model = _dataArray[_selectType];
    kNSUDefaultSaveVauleAndKey(model.printTypeId, @"PrintTypeId");
    BoxSelectViewController *vc = [[BoxSelectViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getData {
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@/get_print_type_list",Auto_REQUESTHEADER] andParameter:nil success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"0"]) {
            [self.dataArray removeAllObjects];
            NSError *error = nil;
            for (NSDictionary *dict in successResponse[@"data"]) {
                PrintTypeModel *model = [[PrintTypeModel alloc] initWithDictionary:dict error:&error];
                model.descriptionStr = dict[@"description"];
                model.printTypeId = [NSString stringWithFormat:@"%@",dict[@"id"]];
                if (kNSUDefaultReadKey(@"PrintTypeId")) {
                    if ([model.printTypeId isEqualToString:kNSUDefaultReadKey(@"PrintTypeId")]) {
                        self.selectType = self.dataArray.count;
                    }
                }
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PrintTypeTableViewCell *cell = [PrintTypeTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
    if (indexPath.row == _selectType) {
        [cell setStatus:YES andModel:_dataArray[indexPath.row]];
    }else {
        [cell setStatus:NO andModel:_dataArray[indexPath.row]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _selectType = indexPath.row;
    [_tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return ScreenX375(53);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(53))];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *rline = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(15), ScreenX375(3), ScreenX375(23))];
    rline.backgroundColor = ThemeColor;
    [view addSubview:rline];
    
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(300), ScreenX375(24))];
    noteLabel.text = @"印刷方式";
    noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
    noteLabel.font = Kfont(18);
    [view addSubview:noteLabel];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(53) - 0.5, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = LineColor;
    [view addSubview:line];
    
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
